﻿/*
 人员头像截图
*/

var HeadImgCrop = angular.module("HeadImgCrop", ['Common', 'ngImgCrop']);
HeadImgCrop.directive('headImgCrop', [function () {
    return {
        restrice: "AE",
        scope: {
            imgShow: "=", //打开头像截取
            onSave: "&",      //保存对象到服务器           
            imgUrl: "=", //文件初始化url地址 如果传空则为一张默认的图片
            resUrl: "=" //返回的base64 编码的字符串
        },
        templateUrl: "/system/directive/personnel/img-crop/head_img_crop.html",
        link: function ($scope, $element, $attrs) {

            // 人员头像
            //$scope.myImage = '';
            $scope.resUrl = "";
            $scope.myImage = function (url) {
                if (url) {
                    if (url.indexOf("?") > 0) { //图片不能带任何参数
                        url = url.substring(0, url.indexOf("?"))
                    }
                    return url
                } else {
                    return "/images/avatar.png";
                }
            }

            $scope.handleFileSelect = function (evt) {
                var file = evt.currentTarget.files[0];
                var reader = new FileReader();
                reader.onload = function (evt) {
                    $scope.$apply(function () {
                        $scope.imgUrl = evt.target.result;
                    });
                };
                reader.readAsDataURL(file);
            };
            //打开文件选择
            $scope.imgFileOpen = function () {
                document.getElementById("personlfileimg").click();
            }
            // 监听文件选择事件
            angular.element(document.querySelector('#personlfileimg')).on('change', $scope.handleFileSelect);
            // 关闭
            $scope.close = function () {
                $scope.onSaveImg();
            }
            $scope.onSaveImg = function () {
                $scope.onSave();
            }

            $scope.onChange = function ($dataURI) {
                console.log('改变', $dataURI);
            };
            $scope.onLoadBegin = function () {
                console.log('开始');
            };
            $scope.onLoadDone = function () {
                console.log('完成');
            };
            $scope.onLoadError = function () {
                console.log('出问题');
            };

        }
    }
} ]);

